#include <QCoreApplication>
#include <QLogger>
#include <QShell>

int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);
  // create a parser
  QShell shell("QShell Demo");
  // add specified type of variable.
  // 1st argument is long name
  // 2nd argument is short name (no short name if '\0' specified)
  // 3rd argument is description
  // 4th argument is mandatory (optional. default is false)
  // 5th argument is default value  (optional. it used when mandatory is
  // false)
  shell.add<QString>("host", 'h', "host name", true, "");

  // 6th argument is extra constraint.
  // Here, port number must be 1 to 65535 by cmdline::range().
  shell.add<int>("port", 'p', "port number", false, 80,
                 cmdline::range(1, 65535));

  // cmdline::oneof() can restrict options.
  shell.add<QString>("type", 't', "protocol type", false, "http",
                     cmdline::oneof<QString>("http", "https", "ssh", "ftp"));

  // Boolean flags also can be defined.
  // Call add method without a type parameter.
  shell.add("gzip", '\0', "gzip when transfer");

  // Add Sub Command
  shell.addSubCommand("check")
      .add<QString>("item", 'i', "check one arg from cmdline", true, "host",
                    cmdline::oneof<QString>("host", "port", "type"))
      .addCallBack([&shell](QCmdParser *p) {
        // Use p->get() to get parameter of this subcommand
        QString type_name = p->get<QString>("item");

        QString info("Check result: ");
        if (type_name == "host") {
          // Use shell.get() to get parameter from arguments
          info += shell.get<QString>(type_name);
        }
        if (type_name == "port") {
          info += QString::number(shell.get<int>(type_name));
        }
        if (type_name == "type") {
          info += shell.get<QString>(type_name);
        }

        QLogger::Info(info);
      });

  // Run parser.
  // It returns only if command line arguments are valid.
  // If arguments are invalid, a parser output error msgs then exit program.
  // If help flag ('--help' or '-?') is specified, a parser output usage
  //  message
  // then exit program.
  shell.parse(argc, argv);
  return a.exec();
}